package bottle.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import bottle.jack.Location;
import bottle.logic.BottleLogic;

/**
 * Servlet implementation class TestServlet.
 */
@WebServlet("/TestServlet")
public class TestServlet extends HttpServlet {
  /**
   * The ID.
   */
  private static final long serialVersionUID = 1L;
  /**
   * The logic behind the server.
   */
  private BottleLogic bottleLogic;

  /**
   * @see HttpServlet#HttpServlet()
   */
  public TestServlet() {
    super();
    bottleLogic = new BottleLogic();
  }

  /**
   * Sets up the server.
   */
  public final void init() {
    bottleLogic.calcMessageId();
  }
  /**
   * @see HttpServlet#doGet(HttpServletRequest request,
   *  HttpServletResponse response)
   *  The HTTP Get method.  Isn't used yet.
   *  @param request the http request.
   *  @param response the http response.
   *  @throws ServletException a servlet exception.
   *  @throws IOException exception thrown if IO messes up.
   */
  protected final void doGet(final HttpServletRequest request,
      final HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.print("Hello.");
  }

  /**
   * @see HttpServlet#doPost(HttpServletRequest request,
   *  HttpServletResponse response)
   *  The HTTP Post method.  This handles all of the requests from the client
   *  side application.
   *  @param request the http request, in this case essentially JSON.
   *  @param response the http response, in this case essentially JSON.
   *  @throws ServletException exception thrown if the servlet is baaaad.
   *  @throws IOException exception thrown if IO messes up.
   */
  protected final void doPost(final HttpServletRequest request,
      final HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/json");
    PrintWriter out = response.getWriter();
    JSONObject json = new JSONObject();
    json.put("message", "Post recieved!");

    try {
      JSONObject jsonReq = new JSONObject(request.getHeader("json"));
      String requestType = jsonReq.getString("type");
      String deviceId = jsonReq.getString("deviceID");
      switch(requestType) {
        case("deviceID"):
          json = bottleLogic.dealWithDeviceId(deviceId);
          break;

        case("name"):
          String name = jsonReq.getString("name");
          json = bottleLogic.dealWithName(deviceId, name);
          break;

        case("position"):
          Double latitude = jsonReq.getDouble("latitude");
          Double longitude = jsonReq.getDouble("longitude");
          int accuracy = jsonReq.getInt("accuracy");
          String date = jsonReq.getString("date");
          Location location = new Location(latitude, longitude, accuracy, date);
          json = bottleLogic.dealWithPosition(deviceId, location);
          break;

        case("new"):
          String destinationId = jsonReq.getString("destID");
          String messageContent = jsonReq.getString("messageData");
          Double mLatitude = jsonReq.getDouble("latitude");
          Double mLongitude = jsonReq.getDouble("longitude");
          int mAccuracy = jsonReq.getInt("accuracy");
          String mDate = jsonReq.getString("date");
          Location mLocation = new Location(mLatitude, mLongitude, mAccuracy,
              mDate);
          json = bottleLogic.dealWithNewMessage(deviceId, destinationId,
              mLocation, messageContent);
          break;

        case("add"):
          String messageId = jsonReq.getString("messageID");
          String messageData = jsonReq.getString("messageData");
          json = bottleLogic.dealWithAdd(deviceId, messageId, messageData);
        default:
          break;
      }
    } catch (ParseException e) {
      e.printStackTrace();
    }

    out.print(json);
  }

}
